﻿using System;
using System.Web.Mvc;
using StashyLib;
using StashyWeb.Models;
using Stashy.Models;

namespace StashyWeb.Controllers
{
    public class SmackController : Controller
    {
        private IStashy<Guid> db;

        public SmackController(IStashy<Guid> stashy)
        {
            this.db = stashy;
        }

        public ViewResult Index()
        {
            return View(db.LoadAll<SmackPair>());
        }

        public ViewResult Details(Guid id)
        {
            return View(db.Load<SmackPair>(id));
        }

        public ActionResult Edit(Guid? id)
        {
            var smackpair = id.HasValue ? db.Load<SmackPair>(id.Value) : new SmackPair();
            return View(smackpair);
        }

        [HttpPost]
        public ActionResult Edit(SmackPair smackpair)
        {
            if (ModelState.IsValid)
            {
                if (!smackpair.Id.HasValue) smackpair.Id = db.GetNewId<SmackPair>();
                db.Save<SmackPair>(smackpair, smackpair.Id.Value);
                return RedirectToAction("Index");
            }

            return View(smackpair);
        }

        public ActionResult Delete(Guid id)
        {
            return View(db.Load<SmackPair>(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            db.Delete<SmackPair>(id);
            return RedirectToAction("Index");
        }
    }
}